import Api from '@/services/api.js'
import mixin from '@/mixin/mixin.js'
const app = getApp()
export default {
  mixins: [mixin],
  components: {
  },
  data() {
    return {
      radioInactive: 'http://ltcx-img.oss-cn-beijing.aliyuncs.com/miniprogram/common/radioInactive.png',
      radioActive: 'http://ltcx-img.oss-cn-beijing.aliyuncs.com/miniprogram/common/radioActive.png',
      dataSource: [], // 数据源
      parentObj: {}, // 父级被选中内容
      parentIdx: 0, // 父级被选中角标
    }
  },
  onLoad(options) {
    this.dataSource = JSON.parse(options.dataSource);
    // 选中数据回显
    const checkedData = JSON.parse(options.checkedData);
    if(Object.keys(checkedData).length) {
      this.parentObj = checkedData;
    }
  },
  onShow() {
  },
  computed: {
    //状态栏高度
    statusBarHeight() {
      let { statusBarHeight } = app.globalData.systemInfo
      return statusBarHeight
    },
    //窗口宽度
    windowWidth() {
      let { windowWidth } = app.globalData.systemInfo
      return windowWidth;
    },
  },
  async mounted() {
  },
  methods: {
    handleLeftItem(e) {
      const { item, index } = e.currentTarget.dataset;
      // 选中相同的数据 不做操作
      if (this.parentObj.id == item.id) return;
      this.parentObj = item;
      this.parentIdx = index;
    },
    // 右侧列表点击事件
    handleRightItem(e) {
      const { item, index } = e.currentTarget.dataset;
      this.parentObj.children[index].isChecked = !(item?.isChecked || false);
    },
    // 底部按钮点击事件
    handleTap({ type }) {
      if (type == 'confirm') {
        this.parentObj.typeCodes = this.parentObj.children.reduce((p, n) => {
          if (n.isChecked) {
            p.push(n.code)
          }
          return p;
        }, []);
        console.log('===============>', this.parentObj);
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.emit('childTabsFilterCallback', this.parentObj)
        uni.navigateBack();
      }else {
        this.parentObj.children.map(i => {
          i.isChecked = false;
        })
      }
    }
  }
}